package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import beans.HttpConfig;
import net.QueryThread;
import net.sf.json.JSONObject;
import portal.controller.BaseServlet;
import portal.dao.ScheduleJobDetailDao;
import portal.engine.JobThread;
import utils.Log4jInit;

/**
 * Servlet implementation class JobCallBack
 */
public class JobCallBack extends BaseServlet {
	private static final long serialVersionUID = 1L;
	//@Autowired
   //protected ScheduleJobDetailDao scheduleJobDetailDao;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JobCallBack() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject resultJson = new JSONObject();
		try {
			String taskHash = request.getParameter("taskId");
			String algorithmHash = request.getParameter("algorithmId");
			String jobId = request.getParameter("jobId");
            String status = request.getParameter("status");
            String completedTime = request.getParameter("completedTime");
            String log = request.getParameter("log");
            Log4jInit.logger.info("callback:    status:"+status +" jobId:"+ jobId +" log"+ log +" algorithmHash:"+algorithmHash+" taskHash:" +taskHash +" completedTime:"+completedTime);

		//	Boolean isScheduleJob = jobDao.isScheduleJobByJobId(jobId);
            //判断任务hash和算法hash是否一致，从而确定是否为调度作业，减少数据库操作
            //为真，表示调度的作业， 否则表示算法作业
        	Boolean isScheduleJob = algorithmHash.equals(taskHash);

        	if(isScheduleJob){
                resultJson.put("result", "success");
                resultJson.put("message", "");
                response.getWriter().println(resultJson.toString());
                new JobThread(taskHash,jobId,algorithmHash,status).start();
            }else {
                resultJson.put("result", "success");
                resultJson.put("message", "");
                response.getWriter().println(resultJson.toString());
                new QueryThread(taskHash,jobId,algorithmHash,status).start();
            }

		} catch (NullPointerException e) {
			e.printStackTrace();
			resultJson.put("result", "failed");
			resultJson.put("message", "POST参数错误");
			response.getWriter().println(resultJson.toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			resultJson.put("result", "failed");
			resultJson.put("message", "状态码参数（status）错误");
			response.getWriter().println(resultJson.toString());
		}
	}

}